package cn.razgriz.controller;

import cn.razgriz.domain.User;
import cn.razgriz.exception.MyException;
import cn.razgriz.service.UserService;
import cn.razgriz.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
public class UserServlet {

    @RequestMapping("/quick")
    public String save(){
        System.out.println("被调用controller的save方法");
        return "/index.jsp";
    }

    @RequestMapping("/quick2")
    public ModelAndView save2(){
        ModelAndView modelAndView = new ModelAndView();
        //model:模型：用来封装数据
        //view:视图：用于展示数据
        //设置视图名称
        modelAndView.setViewName("/index.jsp");
        //设置模型数据
        modelAndView.addObject("username","razgriz");
        return modelAndView;
    }

    @RequestMapping("/quick3")
    public void save3(HttpServletResponse response)throws Exception {
        response.getOutputStream().write("Hello SSM!".getBytes("UTF-8"));
        response.getOutputStream().close();
    }

    @RequestMapping("/quick4")
    @ResponseBody
    public String save4()throws Exception {
        return "Hello world!";
    }


    @RequestMapping("/quick7")
    @ResponseBody
    //spring自动匹配类封装对象
    public void save7(String username, int age)throws Exception {
        System.out.println(username);
        System.out.println(age);
    }


    @RequestMapping("/user/{username}")
    @ResponseBody
    //spring自动匹配类封装对象
    public void save9(@PathVariable(value = "username") String username)throws Exception {

    }

    @RequestMapping("/quick10")
    @ResponseBody
    //spring自动匹配类封装对象
    public void save10(Date date)throws Exception {
        System.out.println(date);
    }

    @RequestMapping("/quick11")
    @ResponseBody
    //spring自动匹配类封装对象
    public void save11(@CookieValue(value = "JSESSIONID",required = false) String cookie)throws Exception {
        System.out.println(cookie);
    }

    @RequestMapping("/target")
    //spring自动匹配类封装对象
    public ModelAndView show1() throws Exception {
        System.out.println("目标资源被执行");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","mobius");
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }

    @RequestMapping("/show")
    //spring自动匹配类封装对象
    public String show(@RequestParam(value="username",required = false)String name) throws Exception {
        UserService service = new UserServiceImpl();
        //数字转换异常
        //Integer i = Integer.valueOf(name);
        //自动抛出异常
        ((UserServiceImpl) service).throwMyException();
        return "/show.jsp";
    }




}
